package com.shencai.demo.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shencai.demo.base.common.BaseContext;
import com.shencai.demo.base.entity.RoleUser;
import com.shencai.demo.base.entity.SysRole;
import com.shencai.demo.base.entity.SysUser;
import com.shencai.demo.base.entity.SysUserRole;
import com.shencai.demo.base.service.SysUserRoleService;
import com.shencai.demo.base.service.SysUserService;
import com.shencai.fw.exception.BaseException;
import com.shencai.fw.web.JsonResult;
import com.shencai.fw.web.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author:Wangym
 * @Date: 2022/10/18  9:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService userService;
    @Autowired
    private SysUserRoleService userRoleService;
    /**
     * 查全部用户
     * @return
     */
    @RequestMapping("/listUser")
    public JsonResult<SysUser> ListUser(){
        JsonResult<SysUser> result = null;
        List<SysUser> user = null;
        try {
            user = userService.list();
        } catch (Exception exception) {
            throw new RuntimeException("查询异常");
        }
        result = new JsonResult<SysUser>();
        result.setSuccess(true, "查询成功");
        result.setData(user);
        return result;
    }

    /**
     * 新增用户
     * @return
     */
    @PostMapping("/addUser")
    public JsonResult<SysUser> addUser(UserInfo userInfo, @RequestBody SysUser sysUser){
        BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
        JsonResult<SysUser> result = null;
        boolean save = false;
        save = userService.saveAccount(sysUser);
        if (save){
            result = new JsonResult<SysUser>(true,200,"添加成功");
            result.setObj(sysUser);
        }else {
            result = new JsonResult<SysUser>(false,200,"添加失败");
        }
        return result;
    }

    /**
     * 编辑用户
     * @param
     * @param
     * @return
     */
    @PostMapping("/updateUser")
    public JsonResult updateUser(UserInfo userInfo, @RequestBody SysUser sysUser){
        try{
            System.out.println(sysUser.toString());
            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            boolean remove = userService.updateById(sysUser);
            if (remove) {
                JsonResult result = new JsonResult<SysUser>(true, 200, "修改成功");
                return result;
            }
            JsonResult result = new JsonResult<SysUser>(false, 444, "修改失败");
            return result;
        } catch (Exception exception) {
            throw new BaseException("修改异常，请稍后重试~");
        }
    }
    /**
     * 删除用户
     */
    @PostMapping("/deleteUser/{id}")
    public JsonResult deleteUser(UserInfo userInfo, @PathVariable("id") Integer id) {
        try{
            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            boolean remove = userService.removeById(id);
            if (remove) {
                JsonResult result = new JsonResult<SysUser>(true, 200, "删除成功");
                return result;
            }
            JsonResult result = new JsonResult<SysUser>(false, 444, "删除失败");
            return result;
        } catch (Exception exception) {
            throw new BaseException("删除异常，请稍后重试~");
        }


    }

    /**
     * 模糊查询
     */
    @RequestMapping("/selectByUser")
    public JsonResult selectByUser(@RequestBody SysUser user) {
        try{
            List sysUsers = userService.getSysUser(user.getAccount(), user.getUsername());
            if (sysUsers!=null) {
                JsonResult result = new JsonResult(true, 200, "查询成功");
                result.setData(sysUsers);
                return result;
            }
            JsonResult result = new JsonResult(false, 444, "查询失败");
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常，请稍后重试~");
        }

    }

    /**
     * 授权模糊查询分页
     */
    @RequestMapping("/listLikeUserRoleByPage")
    public JsonResult<Page<SysUser>> getUserByPage( @RequestBody SysUser user, Integer currentPage, Integer pageSize) {
        try {
            Page<SysUser> users = userService.ListRoleBypage(user.getAccount(),user.getPhone(),user.getUsername(),currentPage,pageSize);
            JsonResult<Page<SysUser>> result = new JsonResult<Page<SysUser>>(true, 200, "查询成功");
            System.out.println(result);
            result.setObj(users);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常");
        }
    }

    /**
     * 用户授权
     * @param
     * @return
     */
    @RequestMapping("/userRoleCommit")
    public JsonResult<SysUserRole> returnRole(UserInfo userInfo,@RequestBody RoleUser roleUser) {
        BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
        JsonResult<SysUserRole> userJasonResult = new JsonResult<>();
        List<SysUserRole> sysUserRoles = new ArrayList<>();
        userJasonResult.setCode(200);
        userJasonResult.setSuccess(true);
        userJasonResult.setMsg(JsonResult.OP_SUCCESS);
        for (SysRole role : roleUser.getSysRoles()) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(roleUser.getUser().getId());
            sysUserRole.setRoleId(role.getId());
            sysUserRoles.add(sysUserRole);
        }
        userRoleService.saveBatch(sysUserRoles);
        userJasonResult.setData(sysUserRoles);
        return userJasonResult;
    }


    @RequestMapping("/userRoleByUserId/{userId}")
    public JsonResult userRoleByUserId(@PathVariable("userId") Integer userId){
        List<SysUserRole> sysUserRoles = userRoleService.userRoleByUserId(userId);
        JsonResult result = new JsonResult();
        result.setSuccess(true);
        result.setData(sysUserRoles);
        System.out.println(sysUserRoles);
        return result;
    }

}
